<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$merchid=plugin('jy_merch')->checklogin();
$setting=$this->plugin_setting('setting'.$merchid);
if($_RQ['op']=='list'){
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'pluginname'=>'jy_merch');
	$return['params']=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	$pindex = max(1, $_RQ['page']);
	$psize = 15;
	$total='';
	$list=pdo_getslice('jy_feieyun_print',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['ismerch']=$item['merchid']?1:0;
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'打印机列表'),
			array('op'=>'edit','title'=>'添加打印机'),
			array('op'=>'cloud','title'=>'批量获取可用打印机'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'ismerch','title'=>'商户的？','type'=>'radio','options'=>'ismerch'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'title','title'=>'打印机名称','type'=>'string'),
			array('name'=>'sn','title'=>'打印机编号sn','type'=>'string'),
			array('name'=>'key','title'=>'打印机识别码key','type'=>'string'),
			array('name'=>'carnum','title'=>'流量卡号','type'=>'string'),
		),'action'=>array(
			array('op'=>'test','title'=>'打印测试联','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	$return['ismerch']=array('平台的','商户的');
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='test'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择打印机！','error');
	}
	$data=<<<TPL
<CB>操作说明<BR></CB>
请登陆网站或扫描打印机上的二维码关注“飞蛾物联”公众号查看设置教程<BR>

<CB>打印机测试页<BR></CB>
名称　       单价   数量   金额<BR>
-------------------------------<BR>
玉米猪肉饺　 17.0   1      17.0<BR>
牛肉粉丝汤   13.0   1      13.0<BR>
-------------------------------<BR>
您好，看到这份单据时表示打印机添加成功，可以正常使用。<BR>
东莞市飞蛾智能科技有限公司<BR>
网址：www.xxxxx.com<BR>
操作说明在网站的设置页，比如：<BR>
1、打印机安装说明<BR>
2、WiFi版打印机设置步骤<BR>
3、飞蛾平台操作说明<BR>
4、微盟、美团、饿了么对接飞蛾打印机说明<BR>
5、查询物联卡流量使用情况、到期日期<BR>
6、设置字体大小、提示音、切纸，打印二维码
TPL;
	$res=$this->print($id,$data);
	if(is_error($res)){
		exi($res['message'],'error');
	}
	exi('打印成功！');
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$data=array(
			'title'=>trim($postdata['title']),
			'sn'=>trim($postdata['sn']),
			'key'=>trim($postdata['key']),
			'carnum'=>trim($postdata['carnum']),
			'storeid'=>trim($postdata['storeid']),
			'merchid'=>trim($postdata['merchid']),
		);
		$id=intval($postdata['id']);
		if($id){
			$result=$this->model('jinyunzn',$setting)->printerEdit($data['sn'],$data['title'],$data['carnum']);
			pdo_update('jy_feieyun_print',$data,array('id'=>$id));
			if($result['ret']!=0){
				exi($result['msg'],'error');
			}
			exi('打印机修改成功！','','goback');
		}else{
			if(!$data['sn']){
				exi('请输入打印机序列号！','error');
			}
			if(!$data['key']){
				exi('打印机识别码key！','error');
			}
			$check=pdo_getcolumn('jy_feieyun_print',array('sn'=>$data['sn']),'id');
			if($check){
				exi('指定序列号打印机已存在，请更换！','error');
			}
			$data['uniacid']=$_SESSION['uniacid'];
			$data['pluginname']='jy_merchid';
			$data['merchid']=$merchid;
			$printerContent=$data['sn'].'#'.$data['key'].'#'.$data['title'].'#'.$data['carnum'];
			$result=$this->model('jinyunzn',$setting)->printerAddlist($printerContent);
			pdo_insert('jy_feieyun_print',$data);
			if($result['ret']!=0){
				exi($result['msg'],'error');
			}
			exi('打印机增加成功！','','goback');
		}
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_feieyun_print',array('id'=>$id));
		$read=true;
	}else{
		$item=array();
		$read=false;
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置打印机信息',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回打印机列表'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'打印机编号sn','name'=>'sn', 'help'=>'打印机序列号，可从打印机机身获取'),
			array('type'=>'string','title'=>'打印机识别码key','name'=>'key', 'help'=>'打印机序列号，可从打印机机身获取'),
			array('type'=>'string','title'=>'流量卡号码','name'=>'carnum', 'help'=>''),
			array('type'=>'string','title'=>'打印机名称','name'=>'title', 'help'=>'设置打印机名称，便于识别使用' ),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores'),
		)
	);
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('print',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			if(intval($id)){
				$this->delete('print',array('id'=>intval($id)));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='cloud'){
	//对接飞蛾物联官网获取可用打印机列表
	$printers=$this->model('model')->cloud_list($setting);
	if(is_error($printers)){
		exi($printers['message'],'error','jy_feieyun/setting/');
	}
	if(!$printers){
		exi('您尚未在飞蛾物联官网获激活打印机，无法获取，请先前往添加激活！','error');
	}
	foreach($printers as $printer){
		$check=pdo_getcolumn('jy_feieyun_print',array('sn'=>$printer['seri_num']),'id');
		if(!$check){
			$new=array(
				'uniacid'=>$_SESSION['uniacid'],
				'sn'=>$printer['seri_num'],
				'title'=>$printer['title'],
				'merchid'=>$merchid,
				'pluginname'=>'jy_merch'
			);
			pdo_insert('jy_feieyun_print',$new);
		}
	}
	exi('获取打印机成功！','error');
}
?>